//
//  MSFightGameScene.m
//  MagicSword
//
//  Created by Netease on 15/11/20.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSFightGameScene.h"
#import "MSTileMap.h"
#import "MSEnemySprite.h"
#import "MSEnemyInfo.h"
#import "MSHeroManager.h"
#import "MSHeroSprite.h"
#import "MSSelectSprite.h"
#import "MSAlertFightView.h"
#import "MSGameToolBar.h"
#import "MSAIManager.h"
#import "MSChapterScene.h"
#import "MSFightHeroInfoNode.h"
#import "MSWeaponNode.h"
#import "MSAlertWeaponView.h"
#import "MSSettingAlertNode.h"
#import "MSProgressBar.h"
#import "MSShadowLabel.h"

@interface MSFightGameScene () <MSGameManagerDelegate, UIAlertViewDelegate>
@end

@implementation MSFightGameScene {
    MSTileMap *_map;
    NSInteger _session;
    NSInteger _chapter;
    MSGameManager *_gameManager;
    
    CGPoint   _touchMoveBeignPoint;
    SKSpriteNode *_curTouchedNode;
    MSHeroSprite *_selectedHeroNode;
    MSEnemySprite *_selectedEnemyNode;
    NSArray *_moveArray;
    MSAlertFightView *_alertView;
    MSAlertWeaponView *_alertWeaponView;
    MSSettingAlertNode *_alertSettingView;
    MSGameToolBar *_toolBar;
    CGPoint        _oldHeroPoint;
    SKLabelNode   *_tipLableNode;
    SKSpriteNode  *_tipBackgroundNode;
    SKSpriteNode  *_selectedTipNode;
    
    MSShadowLabel *_roundCoutLabel;
    MSFightHeroInfoNode *_heroInfoNode;
    
    SKSpriteNode  *_settingSprite;
    MSProgressBar *_expProgressBar;
    
    BOOL           _gameOver;
    BOOL           _isInEnemyRound;
}

- (id)initWithSession:(NSInteger)session Chapter:(NSInteger)chapter {
    self = [super init];
    if (self) {
        _session = session;
        _chapter = chapter;
        _gameOver = NO;
        _gameManager = [MSGameManager shareManager];
        _gameManager.delegate = self;
    }
    return self;
}

- (void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    [super didMoveToView:view];
    
    [self initMap];
    _toolBar = [[MSGameToolBar alloc] init];
    _toolBar.zPosition = 2;
    _toolBar.position = CGPointMake(CGRectGetMaxX(self.frame)-_toolBar.size.width/2, _toolBar.size.height/2);
    _toolBar.hidden = YES;
    [self addChild:_toolBar];
    
    _tipLableNode = [SKLabelNode labelNodeWithFontNamed:@"Helvetica"];
    _tipLableNode.fontColor = [UIColor whiteColor];
    _tipLableNode.fontSize = 32;
    _tipLableNode.zPosition = 4;
    _tipLableNode.hidden = YES;
    [self addChild:_tipLableNode];
    
    _tipBackgroundNode = [SKSpriteNode spriteNodeWithColor:UIColorFromRGBA(0x000000, 0.5) size:CGSizeMake(CGRectGetWidth(self.frame), 70)];
    _tipBackgroundNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    _tipBackgroundNode.zPosition = 3;
    _tipBackgroundNode.hidden = YES;
    [self addChild:_tipBackgroundNode];
    
    _selectedTipNode = [SKSpriteNode spriteNodeWithImageNamed:@"main_hero_show"];
    _selectedTipNode.size = CGSizeMake(_gameManager.tileSize.width-5, _gameManager.tileSize.height-5);
    _selectedTipNode.zPosition = 3;
    _selectedTipNode.hidden = YES;
    [_map addChild:_selectedTipNode];
    
    _roundCoutLabel = [MSShadowLabel labelNodeWithFontNamed:@"Helvetica"];
    _roundCoutLabel.fontColor = [UIColor whiteColor];
    _roundCoutLabel.fontSize = 17;
    _roundCoutLabel.zPosition = 4;
    _roundCoutLabel.position = CGPointMake(CGRectGetWidth(self.frame)-40, CGRectGetHeight(self.frame)-20);
    _roundCoutLabel.text = @"第1回合";
    [self addChild:_roundCoutLabel];
    
    _heroInfoNode = [[MSFightHeroInfoNode alloc] init];
    _heroInfoNode.zPosition = 4;
    _heroInfoNode.position = CGPointMake(CGRectGetWidth(_heroInfoNode.frame)/2, CGRectGetHeight(self.frame)-CGRectGetHeight(_heroInfoNode.frame)/2);
    _heroInfoNode.hidden = YES;
    [self addChild:_heroInfoNode];
    
    _settingSprite = [SKSpriteNode spriteNodeWithImageNamed:@"main_setting"];
    _settingSprite.size = CGSizeMake(30, 30);
    _settingSprite.zPosition = 4;
    _settingSprite.alpha = 0.8;
    _settingSprite.name = NodeName_GameSettingNode;
    _settingSprite.position = CGPointMake(CGRectGetWidth(_settingSprite.frame)/2+10, CGRectGetHeight(self.frame)-CGRectGetHeight(_settingSprite.frame)/2-10);
    [self addChild:_settingSprite];
}

- (void)playStory {
    // 剧情，判断是否已经发展过
    
    // 获得英雄
    [_gameManager addHeroWithSession:_session chapter:_chapter];
}

- (void)initMap {
    _map = [_gameManager setMapWithSession:_session chapter:_chapter];
    if (!_map) {
        [ProgressHUD showError:@"地图不存在"];
        [self backScene];
        return;
    }
    
    [self addChild:_map];
    
    for (SKSpriteNode *node in [_gameManager getEnemyArray]) {
        [_map addChild:node];
    }
    
    [self playStory];
    
    for (SKSpriteNode *node in [_gameManager getHeroArray]) {
        [_map addChild:node];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    _touchMoveBeignPoint = positionInScene;
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    _curTouchedNode = touchedNode;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    
    float newY = _map.position.y-(_touchMoveBeignPoint.y - positionInScene.y);
    [_gameManager moveMapTo:newY];
    _touchMoveBeignPoint = positionInScene;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
    if ([_curTouchedNode isEqual:touchedNode]) {
        //点中英雄
        if ([touchedNode isKindOfClass:[MSHeroSprite class]]) {
            if (_selectedHeroNode && _selectedHeroNode!=touchedNode) {
                return;
            }
            _selectedHeroNode = (MSHeroSprite *)touchedNode;
            if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Normal) {
                [_selectedHeroNode setHeroStatus:MSHeroSpriteStatus_Selected];
                [_toolBar setHidden:NO withAnimated:YES];
                [_gameManager moveMapWithSpriteTop:_selectedHeroNode.position.y animated:YES];
                [self showMoveItem];
            } else if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Selected) {
                _oldHeroPoint = _selectedHeroNode.position;
                [self showAttackItemWithPoint:touchedNode.position];
    
            } else if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Runed) {
                [_selectedHeroNode setHeroStatus:MSHeroSpriteStatus_Finish];
                [_toolBar setHidden:YES withAnimated:YES];
                [self removeSelectView];
                [_gameManager moveMaptToNextNormalHero];
                [self checkFinishHeroRound];
            } else if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Finish) {
                // 这里直接显示英雄属性好了
                _selectedHeroNode = nil;
                if (_heroInfoNode.isHidden) {
                    [self showHeroInfo:[(MSHeroSprite *)touchedNode heroInfo]];
                } else {
                    [_heroInfoNode hideWithAnimation];
                }
                return;
            }
        }
        // 点中敌人
        if ([touchedNode isKindOfClass:[MSEnemySprite class]]) {
            _alertView = [[MSAlertFightView alloc] init];
            _alertView.zPosition = 2;
            _alertView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
            [_alertView setEnemySprite:(MSEnemySprite*)touchedNode];
            [self addChild:_alertView];
        }
        
        if ([touchedNode isKindOfClass:[MSSelectSprite class]]) {
            // 移动
            if ([(MSSelectSprite *)touchedNode status] == MSSeleteSpriteStatus_Normal) {
                _oldHeroPoint = _selectedHeroNode.position;
                [_gameManager moveto:[_gameManager CoordFromPoint:touchedNode.position] sprite:_selectedHeroNode completion:^{
                    
                    [self showAttackItemWithPoint:touchedNode.position];
                }];
            } else if ([(MSSelectSprite *)touchedNode status] == MSSeleteSpriteStatus_Attack) {
                _selectedEnemyNode = [_gameManager getEnemyWithPoint:touchedNode.position];
                if (_selectedEnemyNode) {
                    // 攻击
                    _alertView = [[MSAlertFightView alloc] init];
                    _alertView.zPosition = 2;
                    _alertView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
                    [_alertView setEnemySprite:_selectedEnemyNode];
                    [_alertView setHeroWeaponInfo:_selectedHeroNode.heroInfo.weaponList];
                    [self addChild:_alertView];
                }
            }
        }
        
        // 查看武器
        if ([[touchedNode name] isEqualToString:NodeName_AttackLookNode]) {
            MSWeaponNode* weaponNode;
            if ([touchedNode isKindOfClass:[MSWeaponNode class]]) {
                weaponNode = (MSWeaponNode*)touchedNode;
            } else if([touchedNode.parent isKindOfClass:[MSWeaponNode class]]) {
                weaponNode = (MSWeaponNode*)touchedNode.parent;
            }
            if (weaponNode) {
                if (!_alertWeaponView) {
                    _alertWeaponView = [[MSAlertWeaponView alloc] init];
                    [_alertWeaponView setButtonHidden:YES];
                }
                if (_alertWeaponView.parent == nil) {
                    [self addChild:_alertWeaponView];
                }
                [_alertWeaponView setWeaponInfo:[weaponNode weaponInfo]];
                _alertWeaponView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
            }
            
        } else {
            if (!_heroInfoNode.isHidden) {
                [_heroInfoNode hideWithAnimation];
            }
        }
        
        // 返回
        if ([[touchedNode name] isEqualToString:NodeName_HeroBackButtonNode]) {
            [self backUserAction];
        }
        
        // 英雄资料
        if ([[touchedNode name] isEqualToString:NodeName_HeroInfoButtonNode]) {
            if (_heroInfoNode.isHidden) {
                [self showHeroInfo:_selectedHeroNode.heroInfo];
            } else {
                [_heroInfoNode hideWithAnimation];
            }
        }
        if ([[touchedNode name] isEqualToString:NodeName_CloseHeroInfoButtonNode]) {
            [_heroInfoNode hideWithAnimation];
        }
        
        // 关闭武器窗口
        if ([[touchedNode name] isEqualToString:NodeName_CloseAlertNodes]) {
            if (_alertView) {
                [_alertView setHidden:YES];
            }
            if (_alertWeaponView) {
                [_alertWeaponView removeFromParent];
            }
            if (_alertSettingView) {
                [_alertSettingView removeFromParent];
            }
        }
        
        // 设置
        if ([[touchedNode name] isEqualToString:NodeName_GameSettingNode]) {
            if (!_alertSettingView) {
                _alertSettingView = [[MSSettingAlertNode alloc] init];
            }
            if (_alertSettingView.parent == nil) {
                [self addChild:_alertSettingView];
            }
            _alertSettingView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        }
        
        // 结束游戏
        if ([[touchedNode name] isEqualToString:NodeName_DoneWeaponAlertNodes]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确实退出游戏" message:nil delegate:self cancelButtonTitle:@"退出游戏" otherButtonTitles:@"取消", nil];
            
            [alertView show];
            [_alertSettingView removeFromParent];
        }
        
        // 结束回合
        if ([[touchedNode name] isEqualToString:NodeName_StopRoundButtonNode]) {
            [_alertSettingView removeFromParent];
            if (!_isInEnemyRound) {
                for (MSHeroSprite *hero in [_gameManager getHeroArray]) {
                    [hero setHeroStatus:MSHeroSpriteStatus_Finish];
                }
                [self showFinishNode:YES];
            }
        }
        
        // 开始战斗
        if ([[touchedNode name] isEqualToString:NodeName_AttackWithWeapon]) {
            MSWeaponNode* weaponNode;
            if ([touchedNode isKindOfClass:[MSWeaponNode class]]) {
                weaponNode = (MSWeaponNode*)touchedNode;
            } else if([touchedNode.parent isKindOfClass:[MSWeaponNode class]]) {
                weaponNode = (MSWeaponNode*)touchedNode.parent;
            }
            
            if (weaponNode) {
                MSWeaponInfo *weapon = [weaponNode weaponInfo];
                
                [self removeAlertView];
                [self removeSelectView];
                [_toolBar setHidden:YES withAnimated:YES];
                [_selectedHeroNode.heroInfo setCurWeapon:weapon];
                
                MSFightScene *scene = [_gameManager attackEnemy:_selectedHeroNode enemy:_selectedEnemyNode completion:^{
                    [_selectedHeroNode setHeroStatus:MSHeroSpriteStatus_Finish];
                    [_toolBar setHidden:YES withAnimated:YES];
                    
                    if (_selectedEnemyNode.enemyInfo.HP<=0) {
                        NSInteger oldExp = _selectedHeroNode.heroInfo.Experience;
                        [self showProgressFrom:oldExp enemyInfo:_selectedEnemyNode.enemyInfo heroInfo:_selectedHeroNode.heroInfo complection:^{
                            [_gameManager moveMaptToNextNormalHero];
                            [self checkFinishHeroRound];
                        }];
                    } else {
                        [_gameManager moveMaptToNextNormalHero];
                        [self checkFinishHeroRound];
                    }
                    
                }];
                scene.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
                [self addChild:scene];
            }

        }
        
    }
}

- (void)showHeroInfo:(MSHeroInfo *)heroInfo  {
    [_heroInfoNode setHeroInfo:heroInfo];
    [_heroInfoNode showWithAnimation];
}

- (void)checkFinishHeroRound {
    _selectedHeroNode = nil;
    BOOL flag = [_gameManager checkFinishHeroRound];
    if (flag && !_isInEnemyRound) {
        [self showFinishNode:YES];
    }
}
- (void)showProgressFrom:(NSInteger)oldExp enemyInfo:(MSEnemyInfo *)enemyInfo heroInfo:(MSHeroInfo *)heroInfo {
    [self showProgressFrom:oldExp enemyInfo:enemyInfo heroInfo:heroInfo complection:nil];
}

- (void)showProgressFrom:(NSInteger)oldExp enemyInfo:(MSEnemyInfo *)enemyInfo heroInfo:(MSHeroInfo *)heroInfo complection:(MSCompletionBlock)block {
    heroInfo.Experience += enemyInfo.experience;
    if (!_expProgressBar) {
        _expProgressBar = [[MSProgressBar alloc] initWithSize:CGSizeMake(200, 30)];
        _expProgressBar.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        [self addChild:_expProgressBar];
    }
    
    [_expProgressBar setHidden:NO];
    _tipBackgroundNode.hidden = NO;
    [_tipBackgroundNode runAction:[SKAction fadeInWithDuration:0.2]];
    
    NSInteger nextExp = [_gameManager getLevelNeedExperience:heroInfo.level];
    float oldProgress = (float)oldExp/nextExp;
    float newProgress = (float)heroInfo.Experience/nextExp;
    [_expProgressBar setProgress:oldProgress isBlood:NO];
    [_expProgressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", heroInfo.Experience, nextExp]];
    _expProgressBar.titleLabelNode.fontSize = 20;
    
    __block id weakSelf = self;
    if (heroInfo.Experience < nextExp) {
        [_expProgressBar setProgressWithAniamtion:newProgress completion:^{
            [weakSelf finishShowProgress];
            if (block) {
                block();
            }
        }];
    } else {
        heroInfo.level += 1;
        heroInfo.Experience -= nextExp;
        
        [_expProgressBar setProgressWithAniamtion:1. completion:^{
            [weakSelf upHeroLevelProgressBar:heroInfo complection:block];
        }];
    }
}

- (void)upHeroLevelProgressBar:(MSHeroInfo *)heroInfo complection:(MSCompletionBlock)block{
    __block id weakSelf = self;
    __block MSFightHeroInfoNode *weakHeroInfoNode = _heroInfoNode;
    __block MSHeroInfo *weakHeroInfo = heroInfo;
    NSInteger nextExp = [_gameManager getLevelNeedExperience:heroInfo.level];
    float newProgress = (float)heroInfo.Experience/nextExp;
    NSInteger newExp = heroInfo.Experience;
    [_heroInfoNode setHeroInfo:heroInfo];
    
    [_expProgressBar setProgress:0. isBlood:NO];
    [_expProgressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", newExp, nextExp]];
    [_expProgressBar setProgressWithAniamtion:newProgress completion:^{
        [weakSelf finishShowProgress];
        // 英雄升级
        [ProgressHUD showSuccess:@"英雄等级升级"];
        [weakHeroInfoNode showWithAnimation];
        [weakHeroInfoNode showLevelUp:[MSLevelUpManager getLevelUpInfo:weakHeroInfo.level heroModel:weakHeroInfo.type]];
        [MSLevelUpManager LevelUp:weakHeroInfo];
        if (block) {
            block();
        }
    }];
}

- (void)finishShowProgress {
    [_tipBackgroundNode runAction:[SKAction sequence:@[[SKAction waitForDuration:0.7],
                                                       [SKAction fadeOutWithDuration:0.1]]] completion:^{
        _tipBackgroundNode.hidden = YES;
        [_expProgressBar setHidden:YES];
    }];
}

- (void)showFinishNode:(BOOL)isHero {
    if (isHero) {
        _isInEnemyRound = YES;
        // 显示我方回合结束
        [self showTipLabelWithString:@"我方回合结束" completion:^{
            [self runEnemyRound];
        }];
    } else {
        _isInEnemyRound = NO;
        // 显示敌方回合结束
        [self showTipLabelWithString:@"敌方回合结束" completion:^{
            [_gameManager reloadHeroRound];
            [_gameManager moveMaptToNextNormalHero];
            [_roundCoutLabel setText:[NSString stringWithFormat:@"第%d回合", _gameManager.roundCount]];
        }];
    }
}

- (void)showTipLabelWithString:(NSString *)string completion:(void (^)())block {
    if (_expProgressBar.isHidden == NO) {
        [_expProgressBar setHidden:YES];
    }
    
    _tipBackgroundNode.hidden = NO;
    [_tipBackgroundNode removeAllActions];
    [_tipBackgroundNode runAction:[SKAction sequence:@[[SKAction fadeInWithDuration:0.2],
                                                       [SKAction waitForDuration:1.1],
                                                       [SKAction fadeOutWithDuration:0.2]]] completion:^{
        _tipBackgroundNode.hidden = YES;
    }];
    
    _tipLableNode.text = string;
    _tipLableNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetHeight(self.frame)+100);
    _tipLableNode.hidden = NO;
    _tipLableNode.alpha = 1;
    SKAction *action = [SKAction sequence:@[[SKAction moveToY:CGRectGetMidY(self.frame)-10 duration:0.3],
                                            [SKAction waitForDuration:1.2],
                                            [SKAction fadeOutWithDuration:0.2]]];
    [_tipLableNode runAction:action completion:^{
        _tipLableNode.hidden = YES;
        if (block) {
            block();
        }
    }];
}

- (void)runEnemyRound {
    NSArray *enemyAIRound = [_gameManager runEnemyRound];
    _selectedTipNode.hidden = NO;
    
    [self runEnemyRound:enemyAIRound withIndex:0];
}

- (void)runEnemyRound:(NSArray *)aiArray withIndex:(int)index {
    [self removeSelectView];
    if (_gameOver) {
        _selectedTipNode.hidden = YES;
        return;
    }
    
    if (aiArray.count > index) {
        MSAIInfo *info = aiArray[index];
        
        [_gameManager moveMapWithSpriteTop:info.enemy.position.y animated:YES];
        if (index == 0) {
            _selectedTipNode.position = info.enemy.position;
        }
        [_selectedTipNode runAction:[SKAction moveTo:info.enemy.position duration:0.2] completion:^{
            _moveArray = [_gameManager getHeroMoveArray:info.curCoord canMoveValue:info.enemy.enemyInfo.moveDistance];
            for (SKNode *node in _moveArray) {
                [_map addChild:node];
            }
            
            [_gameManager moveto:info.moveCoord sprite:info.enemy completion:^{
                if (info.attackHero) {
                    MSFightScene *scene = [_gameManager attackEnemy:info.attackHero enemy:info.enemy isHeroAttack:NO completion:^{
                        if (info.enemy.enemyInfo.HP<=0) {
                            NSInteger oldExp = info.attackHero.heroInfo.Experience;
                            [self showProgressFrom:oldExp enemyInfo:info.enemy.enemyInfo heroInfo:info.attackHero.heroInfo complection:^{
                                [self runEnemyRound:aiArray withIndex:index+1];
                            }];
                        } else {
                            [self runEnemyRound:aiArray withIndex:index+1];
                        }
                    }];
                    scene.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
                    [self addChild:scene];
                } else {
                    [self runEnemyRound:aiArray withIndex:index+1];
                }
            }];
        }];
    } else {
        _selectedTipNode.hidden = YES;
        [self showFinishNode:NO];
    }
}

- (void)backUserAction {
    if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Normal) {
        _selectedHeroNode = nil;
        [_toolBar setHidden:YES withAnimated:YES];
    } else if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Selected) {
        [_selectedHeroNode setHeroStatus:MSHeroSpriteStatus_Normal];
        [_toolBar setHidden:YES withAnimated:YES];
        [self removeAlertView];
        [self removeSelectView];
        _selectedHeroNode = nil;
    } else if (_selectedHeroNode.heroStatus == MSHeroSpriteStatus_Runed) {
        [_selectedHeroNode setHeroStatus:MSHeroSpriteStatus_Selected];
        [self removeAlertView];
        [self removeSelectView];
        [_selectedHeroNode setPosition:_oldHeroPoint];
        [self showMoveItem];
    }
}

- (void)showAttackItemWithPoint:(CGPoint)point {
    [_selectedHeroNode setHeroStatus:MSHeroSpriteStatus_Runed];
    [self removeSelectView];
    MSHeroInfo *info = [_selectedHeroNode heroInfo];
    _moveArray = [_gameManager getHeroAttackArray:[_gameManager CoordFromPoint:point] canAttackDistance:info.attackDistance];
    for (SKNode *node in _moveArray) {
        [_map addChild:node];
    }
}

- (void)showMoveItem {
    MSHeroInfo *info = [_selectedHeroNode heroInfo];
    _moveArray = [_gameManager getHeroMoveArray:[_gameManager CoordFromPoint:_selectedHeroNode.position] canMoveValue:info.moveDistance];
    for (SKNode *node in _moveArray) {
        [_map addChild:node];
    }
}

- (void)removeAlertView {
    if (_alertView) {
        [_alertView setHidden:YES];
    }
}

- (void)removeSelectView {
    for (SKNode *node in _moveArray) {
        [node removeFromParent];
    }
}

#pragma mark - MSGameManagerDelegate <NSObject>
- (void)gameOver:(BOOL)isWin {
    _gameOver = YES;
    if (isWin) {
        [self showTipLabelWithString:@"游戏获得胜利" completion:^{
            [_gameManager passChapter:_session chapter:_chapter];
            [self backScene];
        }];
    } else {
        // 显示敌方回合结束
        [self showTipLabelWithString:@"游戏失败" completion:^{
           [self backScene];
        }];
    }
}

- (void)backScene {
    [[MSHeroManager shareManager] saveHeroArray];
    [self.view popScene];
}

#pragma mark - UIAlertViewDelegate <NSObject>

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == alertView.cancelButtonIndex) {
        [self backScene];
    }
}
@end
